<?php

namespace app\admin\controller\knscy;

use app\admin\model\SystemConfig;
use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use app\common\service\WeginService;
use think\App;

/**
 * class Company
 * @package app\admin\controller\knscy
 * @ControllerAnnotation(title="企业信息")
 */
class Company  extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemConfig();
    }

    /**
     * @NodeAnotation(title="设置")
     */
    public function index()
    {
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="保存")
     */
    public function save()
    {
        $this->checkPostRequest();
        $post = $this->request->post();
        try {
            foreach ($post as $key => $val) {
                $orm = SystemConfig::where('group', 'company')
                    ->where('name', $key)
                    ->where('uniacid', WeginService::getUniacid())
                    ->find();
                if ($orm) {
                    $this->model
                        ->where('name', $key)
                        ->where('uniacid', WeginService::getUniacid())
                        ->update([
                            'value' => $val,
                        ]);
                }else{
                    $this->model->create([
                        'name' => $key,
                        'group' => 'company',
                        'value' => $val,
                        'uniacid' => WeginService::getUniacid(),
                        'remark' => '自定义表单'
                    ]);
                }

            }
            TriggerService::updateMenu();
            TriggerService::updateSysconfig();
        } catch (\Exception $e) {
            $this->error('保存失败');
        }
        $this->success('保存成功');
    }

}